<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "java.util.*, java.lang.*, java.net.*, java.io.*, java.util.Date, java.text.DecimalFormat" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Twitter Conversation Retrieval</title>
	<link rel="stylesheet" href="general.css" type="text/css" />
</head>
<body>
	<span class="rtop">
	<span class="r1"></span>
	<span class="r2"></span>
	<span class="r3"></span>
	<span class="r4"></span>
	</span>

	<div id="search_div">
	<div id="title">Twitter Conversation Retrieval</div>
	<br/>
	<form id="search" method="POST" action="result.jsp">
		<input type="text" id="text_search" name="text_search" autocomplete="off" tabindex="1" value="<%= request.getParameter("text_search") %>" />
		
		<input type="hidden" id="value_search" name="value_search" value="<%= request.getParameter("value_search") %>" />
		<input type="hidden" id="pop_search" name="pop_search" value="<%= request.getParameter("pop_search") %>" /> 
		<!--input type="hidden" id="pop2_search" name="pop2_search" value="<%= request.getParameter("pop2_search") %>" /-->
		<input type="hidden" id="retweet_search" name="retweet_search" value="<%= request.getParameter("retweet_search") %>" /> 
		<!--input type="hidden" id="timeliness_search" name="timeliness_search" value="<%= request.getParameter("timeliness_search") %>" /--> 
		<input type="hidden" id="density_search" name="density_search" value="<%= request.getParameter("density_search") %>" />
		<input type="hidden" id="page_num" name="page_num" value="1" />
		<!-- input type="hidden" id="multitweet_search" name="multitweet_search" value="<%= request.getParameter("multitweet_search") %>" / -->
		
		<input id="button_search" value="search" type="submit" />
	</form>
 
	<%

	String server = "<specify ConversationServer IP Address>";
	int port = <specify CRSearchServer port>;

	String querystr = request.getParameter("text_search")+"~0.8";

	/*if ( request.getParameter("multitweet_search") != null && request.getParameter("multitweet_search").equals("on") ) {
		//System.out.println("Ricerco solo conversazioni e non tweet singoli");
		querystr += " AND NOT reply:0";
	}*/

	double p1 = Double.parseDouble(request.getParameter("value_search"));
	double p2 = Double.parseDouble(request.getParameter("retweet_search"));
	//double p3 = Double.parseDouble(request.getParameter("timeliness_search"));
	double p4 = Double.parseDouble(request.getParameter("density_search"));
	double p5 = Double.parseDouble(request.getParameter("pop_search"));
	int pnum = Integer.parseInt(request.getParameter("page_num"));

	// CONNECTION
	
	BufferedReader in = null;
	PrintStream serverout = null;
	//DataOutputStream os = null;
	Socket socket = null;
		
	boolean error = false;
int numRes = 0;
		try {
			socket = new Socket(server, port);
			in = new BufferedReader( new InputStreamReader(socket.getInputStream()) );
			serverout = new PrintStream(socket.getOutputStream(), true);
		}
		catch (Exception e) { 
			error = true;
			out.println("Cannot connect to Search Server");
		}
			
		String search;

		if (!error) {
		try {
			serverout.println(querystr.replace("\t"," ") + "\t" + p1 + "\t" + p2 + "\t" + "0" + "\t" + p4 + "\t" + p5 + "\t" + pnum);
	numRes = Integer.parseInt(in.readLine());

%>

	<div id="result_div">
		<div id="result_title">Results for: <span id="result_word"><%= request.getParameter("text_search") %></span> - <%=numRes%> results - page <%=pnum%></div>
		<!-- <span class="ranking_title">Score | Text - Popul. - Dens.</span> -->
		<span class="ranking_title">Score</span>
	</div>

<%		

	while ((search = in.readLine())!=null) {
				String[] entry = search.split("\t");
				double score = Double.parseDouble(entry[0]);
				String id = entry[1];
				String time = entry[2];
				String user = entry[3];
				String text = entry[4];
				String isConversation = entry[5];

				DecimalFormat twoDForm = new DecimalFormat("######.##");
			out.print("<div class=\"result\">");
			//out.print("<a href=\"http://twitter.com/#!/"+r.screen_user+"\" target=\"_blank\"><img class=\"avatar\" src=\""+r.img_user+"\" /></a>");
			out.print("<span class=\"right\">");
			if (isConversation.equals("Y")) out.print("<span class=\"title_tweet\"><a href=\"conversation.jsp?id="+id+"\">"+text+"</a></span>");
			else out.print("<span class=\"title_tweet\">"+text+"</span>");
			out.print("<br/>");
			out.print("<span class=\"date_tweet\">"+ time+"</span>");
			out.print(" - ");
			out.print("<span class=\"author_tweet\">"+ user +"</span>");
			//out.print("<br/>");
			//out.print("<span class=\"reply_tweet\">"+ r.reply+" reply</span>");
			out.print(" - ");
			//out.print("<span class=\"retweet_tweet\">"+ r.n_retweet+" retweet</span>");
			out.print("<span class=\"ranking\">"+ twoDForm.parse(twoDForm.format(score)).doubleValue() +"</span>");
			out.print("<br/>");
			out.print("</span>");
			out.print("</div>");
			}
		}
		catch (Exception e) { 
			error = true;
			//out.println("Cannot get results: " + e.getMessage());
		}
		}

		try {
			serverout.close();
			in.close();
			socket.close();
		} catch (Exception e) {
		}	
%>

	<br/><br/>
	</div>

<%

        if (pnum*10<numRes) {

%>
	<form id="search" method="POST" action="result.jsp">
		<input type="hidden" id="text_search" name="text_search" autocomplete="off" tabindex="1" value="<%= request.getParameter("text_search") %>" />
		
		<input type="hidden" id="value_search" name="value_search" value="<%= request.getParameter("value_search") %>" />
		<input type="hidden" id="pop_search" name="pop_search" value="<%= request.getParameter("pop_search") %>" /> 
		<input type="hidden" id="retweet_search" name="retweet_search" value="<%= request.getParameter("retweet_search") %>" /> 
		<input type="hidden" id="density_search" name="density_search" value="<%= request.getParameter("density_search") %>" />
		<input type="hidden" id="page_num" name="page_num" value="<%=  (pnum + 1) %>" />
		
		<input value="next page" type="submit" />
	</form>
<%
         }
%>
	<span class="rbottom">
	<span class="r4"></span>
	<span class="r3"></span>
	<span class="r2"></span>
	<span class="r1"></span>
	</span>
</body>
</html>
